package com.czy.utilslib.image;

import android.app.Activity;
import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.czy.utilslib.R;

public class ImageLoadUtil {

    // 不为空检查
    private static boolean needAbort(ImageView imageView) {
        if (imageView == null) return true;
        Context context = imageView.getContext();
        if (context == null) return true;
        if (context instanceof Activity) {
            return ((Activity) context).isFinishing();
        }
        return false;
    }

    /**
     * 加载网络图片
     * @param url           网络图片的url
     * @param imageView     需要加载到的imageview
     */
    public static void loadImageViewByUrl(String url, ImageView imageView){
        // 使用 Glide 加载图片
        if(needAbort(imageView)) return;
        if(url == null || url.isEmpty()) return;
        Glide.with(imageView.getContext())
                .load(url)
                .apply(new RequestOptions()
                        .placeholder(R.drawable.icon_default_acatar)
                        .error(R.drawable.icon_default_acatar)
                ).into(imageView);
    }

}
